//
//  Item.m
//  Maze1
//
//  Created by Lin Tian on 10/7/11.
//  Copyright 2011 tina. All rights reserved.
//

#import "Item.h"
#import "Hud.h"
#import "LocalPlayer.h"

@implementation Item

@synthesize idNum = _id;
@synthesize type = _type;
@synthesize texture = _texture;
@synthesize sprite;

-(id)init
{
    if ((self = [super init])) {
        self.idNum = 0;
        self.type = 0;
        self.texture = nil;
        self.sprite = nil;
    }
    return self;
}

-(void)dealloc
{
    if (self.texture) {
        [self.texture release];
        self.texture = nil;
    }
    
    if (self.sprite) {
        [self.sprite release];
        self.sprite = nil;
    }
    
    [super dealloc];
}

-(bool)useItemBy:(LocalPlayer*)player
{
    return YES;  
}

-(bool)initSpriteWithParentNode:(CCNode*)parentNode inPosition:(CGPoint)loc
{
    //self.sprite = [CCSprite spriteWithFile:@"Icon-Small.png"];
    //self.sprite.position = loc;
    //[parentNode addChild:self.sprite];
    return YES;
}

@end

@implementation SpeedUp

-(id)init
{
    if ((self = [super init])) {
        self.type = speedUp;
        self.texture = [NSString stringWithString:@"Icon-Small.png"];
        deltaSpeed = 30;
    }
    return self;
}

-(bool)initSpriteWithParentNode:(CCNode*)parentNode inPosition:(CGPoint)loc
{
    //self.sprite = [CCSprite spriteWithFile:self.texture];
    //self.sprite.position = loc;
    return [(Hud*)parentNode addItemIcon:self];
    
}

-(bool)useItemBy:(LocalPlayer*)player
{
    player.deltaSpeed += deltaSpeed;
    _player = player;
    [_player.itemList removeObject:self];
    [[CCScheduler sharedScheduler] scheduleUpdateForTarget:self priority:2 paused:NO];
    return YES;
}

-(void)update:(ccTime)dt
{
    if(deltaSpeed > 0)
    {
        _player.deltaSpeed--;
        deltaSpeed--;
    }
    if (deltaSpeed == 0) {
        [[CCScheduler sharedScheduler] unscheduleUpdateForTarget:self];
    }
}

@end



@implementation Invisible

-(id)init
{
    if ((self = [super init])) {
        self.type = transparent;
        self.texture = [NSString stringWithString:@"logout.png"];
        opacity = 120;
    }
    return self;
}

-(bool)initSpriteWithParentNode:(CCNode*)parentNode inPosition:(CGPoint)loc
{
    //self.sprite = [CCSprite spriteWithFile:self.texture];
    //self.sprite.position = loc;
    return [(Hud*)parentNode addItemIcon:self];
    
}

-(bool)useItemBy:(LocalPlayer*)player
{
    player.playerSprite.opacity = opacity;
    _player = player;
    [_player.itemList removeObject:self];
    [[CCScheduler sharedScheduler] scheduleUpdateForTarget:self priority:2 paused:NO];
    return YES;
}

-(void)update:(ccTime)dt
{
    if(opacity < 255)
    {
        _player.playerSprite.opacity+=3;
        opacity+=3;
    }
    if (opacity>=255) {
        [[CCScheduler sharedScheduler] unscheduleUpdateForTarget:self];
    }
}

@end